package es.udc.fic.hamNeggs.web;

import org.apache.log4j.Logger;

import es.udc.fic.hamNeggs.dao.InputCategory;
import es.udc.fic.hamNeggs.manager.UserStoriesCategoryManager;
import es.udc.fic.hamNeggs.pojo.UserStoriesCategory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;

/**
 * Servlet implementation class CategoriesManagement
 */
public class CategoriesManagement extends HttpServlet {
    private static final long   serialVersionUID = 1L;
    private final static Logger LOG              = Logger.getLogger(CategoriesManagement.class.getName());

    /**
     * @see HttpServlet#HttpServlet()
     */
    public CategoriesManagement() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final UserStoriesCategoryManager cmtmng = new UserStoriesCategoryManager();

        final ArrayList<InputCategory> categories = new ArrayList<InputCategory>();
        for (final UserStoriesCategory cmt : cmtmng.getCommentList()) {
            final InputCategory category = new InputCategory();
            category.setCategory(cmt);
            categories.add(category);
        }

        cmtmng.close();
        request.setAttribute("categories", categories);
        request.getRequestDispatcher("/categories.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final UserStoriesCategoryManager catmng = new UserStoriesCategoryManager();

        try {
            switch (request.getParameter("action")) {
                case "AddCategory":
                    final UserStoriesCategory category = new UserStoriesCategory();
                    category.setCategory(request.getParameter("name"));
                    catmng.saveUserStoriesCategory(category);
                    break;
                case "UpdateCategory":
                    final UserStoriesCategory categoryu = catmng.getUserStoriesCategory(new Long(request.getParameter("catid")));
                    if (request.getParameter("name") != null && !request.getParameter("name").equals(""))
                        categoryu.setCategory(request.getParameter("name"));
                    catmng.updateUserStoriesCategory(categoryu);
                    break;
                case "DeleteCategory":
                    final UserStoriesCategory categoryd = catmng.getUserStoriesCategory(new Long(request.getParameter("catid")));
                    catmng.deleteUserStoriesCategory(categoryd);
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            String error = "[";
            Enumeration<String> names = request.getParameterNames();
            while (names.hasMoreElements()) {
                final String paramName = names.nextElement();
                final String paramValue = request.getParameter(paramName);
                error = error.concat(paramName + ":" + paramValue + ";");
            }
            error = error.concat("]");
            LOG.error("Error while executing action: " + request.getParameter("action")
                    + "with params: " + error + "\n"
                    , e);
        }

        catmng.close();

        doGet(request, response);
    }

}
